package com.controller;

import com.api.CommonResult;
import com.entity.table.RecommendTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Recommend;
import com.service.RecommendService;

import java.io.Serializable;
import java.util.List;

/**
 * 原色咖啡新品推荐 控制层。
 *
 * @author admin
 * @since 2024-06-18
 */
@Controller
@RequestMapping("/admin/recommend")
public class RecommendController {

    @Autowired
    private RecommendService recommendService;

    /**
     * 添加原色咖啡新品推荐。
     *
     * @param recommend 原色咖啡新品推荐
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save( Recommend recommend) {
        try {
            boolean r = recommendService.save(recommend);
            return CommonResult.success(r);
        } catch (Exception e){
            e.printStackTrace();
            String msg = recommend.getTName()+"已存在";
            return  CommonResult.failed(msg);
        }
    }

    /**
     * 根据主键删除原色咖啡新品推荐。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return recommendService.removeById(id);
    }

    /**
     * 根据主键更新原色咖啡新品推荐。
     *
     * @param recommend 原色咖啡新品推荐
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody Recommend recommend) {
//        return recommendService.updateById(recommend);
//    }

    /**
     * 查询所有原色咖啡新品推荐。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public String goodList(ModelMap model, @RequestParam(value = "tName",defaultValue = "")String tName,
                           @RequestParam(value = "p",defaultValue = "1") int p,
                           @RequestParam(value = "pageSize",defaultValue = "10")int pageSize) {
        //按照用户名模糊查询
        try {
            QueryWrapper queryWrapper = QueryWrapper.create().where(RecommendTableDef.RECOMMEND.T_NAME.like(tName));
            Page<Recommend> page = new Page<>();
            page.setPageSize(pageSize);//设置每页显示记录数
            page.setPageNumber(p);//设置页码
            Page<Recommend> pageInfo = recommendService.getMapper().paginateWithRelations(page,queryWrapper);
            model.addAttribute("page", pageInfo);
            model.addAttribute("total", pageInfo.getTotalRow());
        } catch (Exception e){
            e.printStackTrace();
        }
        return "back/coffee-list";
    }
    /**
     * 根据原色咖啡新品推荐主键获取详细信息。
     *
     * @param id 原色咖啡新品推荐主键
     * @return 原色咖啡新品推荐详情
     */
    @GetMapping("getInfo/{id}")
    public Recommend getInfo(@PathVariable Serializable id) {
        return recommendService.getById(id);
    }

    /**
     * 分页查询原色咖啡新品推荐。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Recommend> page(Page<Recommend> page) {
        return recommendService.page(page);
    }

    @GetMapping(value = "addView")
    public String addView(Model model){

        return "back/coffee-add";
    }

    //添加删除用户代码
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            boolean r = recommendService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e){
            e.printStackTrace();
            return  CommonResult.failed(e.getMessage());
        }
    }

    @GetMapping(value = "updateView")
    public String updateView(int id, Model model){
        List<Recommend> typeList = recommendService.list();
        Recommend recommend = recommendService.getById(id);
        model.addAttribute("typeList",typeList);
        model.addAttribute("bGood",recommend);
        return "back/coffee-edit";
    }

    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Recommend recommend) {
        try {
            boolean r = recommendService.updateById(recommend);
            return CommonResult.success(r);
        } catch (Exception e){
            e.printStackTrace();
            String msg = recommend.getTName()+"已存在";
            return  CommonResult.failed(msg);
        }
    }

}
